// @/api/classReport/index.js

import { ref, reactive, toRefs } from 'vue';
import { getTeacherInfo } from "@/api/teacher/teacher";
import { listClassScore } from '@/api/Classscoreonmonth/Classscoreonmonth';
import { getInfo } from "@/api/login";  // 确保导入 getInfo

export default function useTeacherInfo() {
    const userInfo = reactive({
        role: "",
        userList: { nickName: "", userId: "", userName: "" },
        workList: { classss: "", grade: "", school: "" , yearAdmission : "" }
    });

    const scoreList = ref([]);

    const { role, userList, workList } = toRefs(userInfo);

    const getList = async () => {
        const response = await getInfo();  // 确保 getInfo 函数存在
        if (response.code === 200) {
            role.value = response.roles[0];
            userList.value = {
                nickName: response.user.nickName,
                userName: response.user.userName,
                userId: response.user.userId
            };
            await getTeacher();  // 获取教师信息（班级、年级、学校）
        }
    };

    const getTeacher = async () => {
        const response = await getTeacherInfo(userList.value.userName);
        if (response.code === 200) {
            workList.value = {
                classss: response.data[0].classss,
                school: response.data[0].school,
                grade: response.data[0].grade,
                yearAdmission : response.data[0].yearAdmission
            };
            await getClassScore();  // 获取班级成绩信息
        }
    };

    const getClassScore = async () => {
        const response = await listClassScore(workList.value.yearAdmission, workList.value.school, workList.value.classss);
        scoreList.value = response;  // 更新 scoreList
    };

    const loadData = async () => {
        await getList();
        return scoreList.value;
    };

    return {
        userInfo,
        role,
        userList,
        workList,
        scoreList,
        loadData
    };
}
